package edu.ntu.im.p2ptrade.msg;

import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import edu.ntu.im.p2ptrade.P2PTrade;
import edu.ntu.im.p2ptrade.entity.Item;
import edu.ntu.im.p2ptrade.entity.TransactionRequest;

public class TRequest implements PMessage {
	public static final String PART1OWNER = "part1Owner";


	public static final String PART1IID = "part1iid";

	public static final String PART1INAME = "part1name";

	public static final String PART1IMEMO = "part1memo";
	
	public static final String PART1PICURL = "part1picURL";

	public static final String PART2OWNER = "part2Owner";

	public static final String PART2IID = "part2iid";;

	public static final String PART2INAME = "part2name";

	public static final String PART2IMEMO = "part2memo";
	
	public static final String PART2PICURL = "part2picURL";

	TransactionRequest tr;

	public TRequest(TransactionRequest tr) {
		this.tr = tr;
	}

	public Message getMessageToTransfer() {
		Message msg = new Message();
		msg.addMessageElement(P2PTrade.getInstance().getAppName(), new StringMessageElement(PEER, P2PTrade.getInstance().getPeerName(), null));
		msg.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(TRANSACTIONID, tr.getTrans().getId(),
						null));
		msg.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(PART1OWNER, tr.getTrans().getPart1()
						.getOwner(), null));
		
		for (Item item : tr.getTrans().getPart1().getItems()) {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART1IID, item.getId().toString(), null));
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART1INAME, item.getName(), null));
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART1IMEMO, item.getMemo(), null));
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART1PICURL, item.getPhotoUrl(), null));
		}
		
		msg.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(PART2OWNER, tr.getTrans().getPart2()
						.getOwner(), null));
		
		for (Item item : tr.getTrans().getPart2().getItems()) {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART2IID, item.getId().toString(), null));
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART2INAME, item.getName(), null));
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART2IMEMO, item.getMemo(), null));
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(PART2PICURL, item.getPhotoUrl(), null));
		}
		msg.addMessageElement(P2PTrade.getInstance().getAppName(), new StringMessageElement(TYPE,TRequest.class.getSimpleName(), null));
		return msg;
	}

	public String toLogMessage() {
		StringBuffer sb = new StringBuffer();
		sb.append("Transaction request id["+tr.getTrans().getId()+"]");
		sb.append(" items: ");
		for (Item item:this.tr.getTrans().getPart1().getItems()){
			sb.append(item.toString());
		}
		sb.append(" exchange for ");
		for (Item item:this.tr.getTrans().getPart2().getItems()){
			sb.append(item.toString());
		} 
		return sb.toString();
	}

}
